package com.example.mvvm.rx;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.example.mvvm.R;
import com.example.mvvm.until.RomUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RxPermissionReq {
    private static RxPermissionReq rxPermissionReq;

    public static RxPermissionReq getInstance() {
        if (rxPermissionReq == null) {
            synchronized (RxPermissionReq.class) {
                if (rxPermissionReq == null) {
                    rxPermissionReq = new RxPermissionReq();
                }
            }
        }
        return rxPermissionReq;
    }


    //读写
    public static final String PRE_WRITE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    //相机
    public static final String PRE_CAMERA = Manifest.permission.CAMERA;
    //定位
    public static final String PRE_LOCATION1 = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String PRE_LOCATION2 = Manifest.permission.ACCESS_FINE_LOCATION;
    //录音
    public static final String PRE_RECORD = Manifest.permission.RECORD_AUDIO;
    //手机
    public static final String PRE_PHONE = Manifest.permission.READ_PHONE_STATE;
    //拨打电话
    public static final String PRE_CALL = Manifest.permission.CALL_PHONE;


    public void reqPermission(Activity activity, PermissionListener Listener, String... permissionGroup) {
        new RxPermissions(activity).requestEachCombined(permissionGroup)
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            //全部同意
                            Listener.success();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            showToAppSettingDialog(activity, "授权失败", "前往手机应用权限手动设置权限");
                            Listener.error();
                        } else {
                            //拒绝
                            if (!checkPermission(activity)) {
                                showToAppSettingDialog(activity, "授权失败", "前往手机应用权限手动设置权限");
                                Listener.error();
                            } else {
                                Listener.success();
                            }
                        }
                    }
                });
    }

    public interface PermissionListener {
        void success();
        void error();
    }

    public void startPermissionPage(Context mContext) {
        Intent mIntent = null;
        if (RomUtils.isMiui()) {
            mIntent = RomUtils.MiuiPermission(mContext);
        } else if (RomUtils.isMeizu()) {
            mIntent = RomUtils.MeiZuPermission(mContext);
        } else if (RomUtils.isHuawei()) {
            mIntent = RomUtils.HuaWeiPermission(mContext);
        } else if (RomUtils.isQihoo()) {
            mIntent = RomUtils.QihooPermission(mContext);
        } else if (RomUtils.isSony()) {
            mIntent = RomUtils.SonyPermission(mContext);
        } else if (RomUtils.isOppo()) {
            mIntent = RomUtils.OppoPermission(mContext);
        } else if (RomUtils.isLg()) {
            mIntent = RomUtils.LgPermission(mContext);
        } else {
            mIntent = RomUtils.applicationInfo(mContext);
        }
        mContext.startActivity(mIntent);
    }

    public boolean checkPermission(Context context, String... permissionsGroup) {
        boolean pm = true;
        for (String item : permissionsGroup) {
            int permission = ContextCompat.checkSelfPermission(context, item);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                //没有授权
                pm = false;
                break;
            }
        }
        return pm;

    }

    public void showToAppSettingDialog(final Context context, String title, String tips) {
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(tips)
                .setPositiveButton("设置",
                        (dialog, which) -> {
                            RxPermissionReq.getInstance().startPermissionPage(context);
                        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.exit(0);
                    }
                }).show();
    }

}
